<template>
  <div>
    <h1>英雄列表</h1>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="_id" label="id"> </el-table-column>
      <el-table-column prop="name" label="英雄名称"> </el-table-column>
      <el-table-column label="英雄图标">
        <template slot-scope="scope">
          <div>
            <img :src="scope.row.avatar" alt="" style="height: 3em" />
          </div>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="echo(scope.row._id)"
            >编辑</el-button
          >
          <el-button @click="remov(scope.row._id)" type="text" size="small"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 编辑对话框 -->
    <el-dialog title="提示" :visible.sync="dialogVisible" width="70%">
      <el-form ref="form" :model="editform" label-width="80px">
        <el-form-item label="_id">
          <el-input v-model="editform._id"></el-input>
        </el-form-item>
        <el-form-item label="英雄名字">
          <el-input v-model="editform.name"></el-input>
        </el-form-item>
        <!-- <el-form-item label="英雄头像">
          <img :src="editform.avatar" alt="" style="height: 3em" />
        </el-form-item> -->
        <el-form-item label="英雄头像">
          <el-upload
            class="avatar-uploader"
            :action="$http.defaults.baseURL + '/upload'"
            :headers="getAuthHeaders()"
            :show-file-list="false"
            :on-success="(res) => $set(editform, 'avatar', res.url)"
          >
            <img v-if="editform.avatar" :src="editform.avatar" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="Banner">
          <el-upload
            class="avatar-uploader"
            :action="uploadUrl"
            :headers="getAuthHeaders()"
            :show-file-list="false"
            :on-success="(res) => $set(editform, 'banner', res.url)"
          >
            <img v-if="editform.banner" :src="editform.banner" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="难度">
          <el-rate
            style="margin-top: 0.6rem"
            :max="9"
            show-score
            v-model="editform.scores.difficult"
          ></el-rate>
        </el-form-item>
        <el-form-item label="技能">
          <el-rate
            style="margin-top: 0.6rem"
            :max="9"
            show-score
            v-model="editform.scores.skills"
          ></el-rate>
        </el-form-item>
        <el-form-item label="攻击">
          <el-rate
            style="margin-top: 0.6rem"
            :max="9"
            show-score
            v-model="editform.scores.attack"
          ></el-rate>
        </el-form-item>
        <el-form-item label="生存">
          <el-rate
            style="margin-top: 0.6rem"
            :max="9"
            show-score
            v-model="editform.scores.survive"
          ></el-rate>
        </el-form-item>
        <el-form-item label="顺风出装">
          <el-select v-model="editform.items1" multiple>
            <el-option
              v-for="item of items"
              :key="item._id"
              :label="item.name"
              :value="item._id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="逆风出装">
          <el-select v-model="editform.items2" multiple>
            <el-option
              v-for="item of items"
              :key="item._id"
              :label="item.name"
              :value="item._id"
            ></el-option>
          </el-select>
        </el-form-item>

        <!-- <el-form-item label="英雄称号">
          <el-input v-model="editform.title"></el-input>
        </el-form-item> -->
        <el-form-item label="使用技巧">
          <el-input type="textarea" v-model="editform.usageTips"></el-input>
        </el-form-item>
        <el-form-item label="对抗技巧">
          <el-input type="textarea" v-model="editform.battleTips"></el-input>
        </el-form-item>
        <el-form-item label="团战思路">
          <el-input type="textarea" v-model="editform.teamTips"></el-input>
        </el-form-item>

        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        <div v-for="(item, index) in editform.skills" :key="index">
          <el-form-item label="英雄技能">
            <el-input type="textarea" v-model="item.name"></el-input>
          </el-form-item>
          <el-form-item label="冷却值">
            <el-input v-model="item.delay"></el-input>
          </el-form-item>
          <el-form-item label="消耗">
            <el-input v-model="item.cost"></el-input>
          </el-form-item>
          <el-form-item label="描述">
            <el-input v-model="item.description" type="textarea"></el-input>
          </el-form-item>
          <el-form-item label="小提示">
            <el-input v-model="item.tips" type="textarea"></el-input>
          </el-form-item>
          <el-form-item label="技能头像">
            <el-upload
              class="avatar-uploader"
              :action="$http.defaults.baseURL + '/upload'"
              :headers="getAuthHeaders()"
              :show-file-list="false"
              :on-success="(res) => $set(item, 'icon', res.url)"
            >
              <img v-if="item.icon" :src="item.icon" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
        </div>

        <div>
          <el-button size="small" @click="editform.skills.push({})">
            <i class="el-icon-plus"></i> 添加英雄技能
          </el-button>
          <el-row type="flex" style="flex-wrap: wrap">
            <!-- 原本是空数组 通过上面@click="form.skills.push(push({}))添加技能展示" -->
            <el-col :md="12" v-for="(item, i) in editform.skills" :key="i">
              <el-form-item label="名称">
                <el-input v-model="item.name"></el-input>
              </el-form-item>
              <el-form-item label="技能头像">
                <el-upload
                  class="avatar-uploader"
                  :action="$http.defaults.baseURL + '/upload'"
                  :headers="getAuthHeaders()"
                  :show-file-list="false"
                  :on-success="(res) => $set(item, 'icon', res.url)"
                >
                  <img v-if="item.icon" :src="item.icon" class="avatar" />
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </el-form-item>
              <el-form-item label="冷却值">
                <el-input v-model="item.delay"></el-input>
              </el-form-item>
              <el-form-item label="消耗">
                <el-input v-model="item.cost"></el-input>
              </el-form-item>
              <el-form-item label="描述">
                <el-input v-model="item.description" type="textarea"></el-input>
              </el-form-item>
              <el-form-item label="小提示">
                <el-input v-model="item.tips" type="textarea"></el-input>
              </el-form-item>

              <!-- 删除英雄一列  splice(i,1)第一个参数是指定要删除的位置  第二个参数是要删几个-->
              <el-form-item>
                <el-button
                  size="small"
                  type="danger"
                  @click="editform.skills.splice(i, 1)"
                  >删除</el-button
                >
              </el-form-item>
            </el-col>
          </el-row>
        </div>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="edit()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
 <script>
export default {
  data() {
    return {
      items: [],
      tableData: [],
      dialogVisible: false,
      editform: {
        name: "",
        _id: "",
        avatar: "",
        banner: "",
        //英雄技能
        skills: {
          name: "",
        },
        // 英雄评分
        scores: {
          difficult: 0,
          skills: 0,
          attack: 0,
          survive: 0,
        },
        items1: "",
        items2: "",
      },
    };
  },
  created() {
    this.getlist();
    this.fetchItems();
    // this.fetchHeroes()
  },
  methods: {
    async getlist() {
      const res = await this.$http.get("/rest/heroes");
      this.tableData = res.data;
      console.log(res);
    },
    // 删除
    async remov(id) {
      // 弹框提示用户，是否要删除数据
      const confirmResult = await this.$confirm(
        "此操作将永久删除该用户, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);

      // 用户取消了删除
      if (confirmResult !== "confirm") {
        return this.$message({
          type: "info",
          message: "已取消删除",
        });
      }
      // console.log(id);
      const res = await this.$http.delete(`/rest/heroes/${id}`);
      console.log(res);
      this.$message.success = "成功删除";
      this.getlist();
      // this.dialogVisible = true;
    },
    // 回显
    async echo(id) {
      this.dialogVisible = true;
      const res = await this.$http.get(`/rest/heroes/${id}`);
      console.log(res);
      this.editform = Object.assign({}, this.form, res.data);
    },

    // 修改
    async edit() {
      const res = await this.$http.put(
        `/rest/heroes/${this.editform._id}`,
        this.editform
      );
      this.dialogVisible = false;
      console.log(res);
      this.getlist();
    },

    //已经建好的物品
    async fetchItems() {
      const res = await this.$http.get(`rest/items`);
      this.items = res.data;
    },

    afterUpload(res) {
      console.log(res);
      // 显示赋值 vue提供的 明确告诉vue 我们要在form上添加一个icon属性
      this.$set(this.form, "icon", res.url);
      this.form.avatar = res.url;
    },

    // // 这里是已经建好的英雄 添加英雄技能的时候要恋情
    //     async fetchHeroes() {
    //   const res = await this.$http.get(`rest/heroes`);
    //   this.heroes = res.data;
    // }
  },
};
</script>